/*========注册检测=======*/
	var oRegister = document.getElementById("register");
	var oUsername = document.getElementById("username");
	var oEmail = document.getElementById("email");
	var oPassword = document.getElementById("password");
	var oSurePassword = document.getElementById("sure-password");
	var oSecurityCode = document.getElementById("security-code");
	var oGetSecurityCode = document.getElementById("get-security-code");
	var oRegisterBtn = document.getElementById("register-btn");
	var oUsernameTip = document.getElementById("username-tip");
	var oEmailTip = document.getElementById("email-tip");
	var oPasswordTip = document.getElementById("password-tip");
	var oSureTip = document.getElementById("sure-tip");
	var oSecurityTip = document.getElementById("security-tip");
	var oRegisterTip = document.getElementById("register-tip");
	var emailExist = 0;
	//验证事件

	oUsername.focus();
	oUsername.onkeyup = function(){checkUsername(oUsername,oUsernameTip)};
	oUsername.onblur = function(){checkUsername(oUsername,oUsernameTip)};
	oEmail.onkeyup = function(){checkEmail(oEmail,oEmailTip)};
	oEmail.onblur = function(){checkEmail(oEmail,oEmailTip)};
	oPassword.onkeyup = function(){checkPassword(oPassword,oPasswordTip,oSurePassword,oSureTip)};
	oSurePassword.onkeyup = function(){checkSurePassword(oPassword,oSurePassword,oSureTip)};
	oSecurityCode.onkeyup = function(){checkSecurityCode(oSecurityCode,oSecurityTip)};
	//点击注册按钮

	oRegisterBtn.onclick = register;
	//注册

	function register(){
		if(emailExist == 1){
			if(checkSecurityCode(oSecurityCode,oSecurityTip) && checkUsername(oUsername,oUsernameTip) && checkEmail(oEmail,oEmailTip) && checkPassword(oPassword,oPasswordTip,oSurePassword,oSureTip) && checkSurePassword(oPassword,oSurePassword,oSureTip)){
				ajax({
					type : "post",
					url : "/cyl/register",
					data : {
						"username" : oUsername.value,
						"email" : oEmail.value,
						"password" : oPassword.value,
                  "email_captcha": oSecurityCode.value
					},
					success : registerSendSuccess,
					error : registerSendError
				});
			}
		}else if(emailExist == -1){
			oEmailTip.style.color = "#f63";
			oEmailTip.innerHTML = "该邮箱已经被注册";
		}else{
			oSecurityTip.style.color = "#f63";
			oSecurityTip.innerHTML = "别急，还没获取验证码呢";
		}

	}
	/*============  注册事件  ==============*/
	function registerSendSuccess(response){
		var respJson = JSON.parse(response);
		console.log(respJson);
		if(respJson.status == 1){
			resetBtn();
			oRegister.innerHTML = "<h1>新用户注册</h1><p>注册成功√<br><a href='http://localhost:8080/cyl/index.html'>跳回主页登录</a></p>"
		}else if(respJson.status == 0){
			oSecurityTip.style.color = "#f63";
			oSecurityTip.innerHTML = "验证码错误";
		}else if(respJson.status == -1){
			oRegisterTip.innerHTML = "非常抱歉，服务器发生错误";
			resetBtn();
		}
	}
	function registerSendError(){
		oRegisterTip.innerHTML = "访问服务器失败";
	}
	/*===============  获取验证码  ===============*/

	oGetSecurityCode.onclick = askSecurityCode;
	var securityCodetimer;
	var time = 60;
	function askSecurityCode(){
		if(checkEmail(oEmail,oEmailTip)){
			oGetSecurityCode.onclick = null;
			securityCodetimer = window.setInterval(setTime,1000);
			ajax({
				type : "post",
				url : "/cyl/EmailVerify",
				data : {
					"ask_type": 1,
					"email" : oEmail.value
				},
				success : askSecurityCodeSuccess,
				error : askSecurityCodeError
			});
		}
	}
	function askSecurityCodeSuccess(response){
		var responseJson = JSON.parse(response);
		if(responseJson.email == 0){
			oEmailTip.style.color = "#f63";
			oEmailTip.innerHTML = "该邮箱已经被注册";
			emailExist = -1;
			resetBtn();
		}else if(responseJson.email == 1){
			emailExist = 1;
			oSecurityTip.style.color = "#0c0";
			oSecurityTip.innerHTML = "获取验证码成功，快去邮箱查看吧";
		}
	}
	function askSecurityCodeError(){
		oRegisterTip.innerHTML = "访问服务器失败";
	}
	function setTime(){
		oGetSecurityCode.innerHTML = time + "s";
		time--;
		if( time == -1 ){
			resetBtn();
		}
	}
	function resetBtn(){
		window.clearInterval(securityCodetimer);
		time = 60;
		oGetSecurityCode.innerHTML = "重新获取";
		oGetSecurityCode.onclick = askSecurityCode;
	}
